<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Advance_payment_model extends CI_Model {
	public function __construct() {
		parent::__construct();
	}

	public function get_status($id){
		return $this->db->query('SELECT ap.prepayment_id, aa.`account_id`, aa.`acct_name`,aa.`acct_num`, SUM(ap.amount) as amount FROM ubs_prepayments ap
								LEFT JOIN ar_account aa ON aa.`account_id` = ap.`account_id` WHERE aa.account_id = "'.$id.'" LIMIT 1')->result();
	}

	public function get_balance($id){
		return $this->db->query('SELECT ap.prepayment_id, aa.`account_id`, aa.`acct_name`,aa.`acct_num`, ap.`amount` FROM ubs_prepayments ap
								LEFT JOIN ar_account aa ON aa.`account_id` = ap.`account_id` WHERE ap.account_id = "'.$id.'" ')->result();
	}

	public function get_prepayments($id){
		return $this->db->query('SELECT ap.prepayment_id, aa.`account_id`,ap.prepayment_no, ap.paytype, ap.ref_no, aa.`acct_name`,aa.`acct_num`, ap.`amount`,ap.created_date FROM ubs_prepayments ap
								LEFT JOIN ar_account aa ON aa.`account_id` = ap.`account_id` WHERE ap.account_id = "'.$id.'" ')->result();
	}

	public function get_cust_all(){
		return $this->db->query('SELECT * FROM ar_account WHERE status = "ACT"')->result();
	}

	public function get_cust_all2(){
		return $this->db->query('SELECT * FROM ar_account WHERE status = "ACT"')->result();
	}

	public function insert($data){
		$this->db->query("call ubs_prepayments_ins(?,?,?,?,?,?,@err,@msg,@no)", $data);
		$que = $this->db->query("SELECT @err as err,@msg as msg, @no as no");
		$msg;
		$err;
		$no;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
			$no = $keys->no;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
			'payment_no' => $no
		);
		echo json_encode($arr);
	}

	public function update($data){
		$this->db->query("call ubs_prepayments_ins(?,?,?,@err,@msg)", $data);
		$que = $this->db->query("SELECT @err as err,@msg as msg");
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
		);
		echo json_encode($arr);
	}

	public function get_paytype($type){
		$this->db->where('lookup_type',$type);
		return $this->db->get('app_lookup_values')->result();
	}


}